package com.example.android_socketcan;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class MyService extends Service {
    android_socketcan can1;
    MyBinder myBinder = new MyBinder();
    public MyService() {
    }

    @Override
    public void onCreate() {
        super.onCreate();
        execRootCmdSilent("ip link set can0 down");
        execRootCmdSilent("ip link set can0 type can bitrate 500000 dbitrate 500000 fd on");
        execRootCmdSilent("ip link set can0 up");

        execRootCmdSilent("ip link set can1 down");
        execRootCmdSilent("ip link set can1 type can bitrate 500000 dbitrate 500000 fd on");
        execRootCmdSilent("ip link set can1 up");
        can1 = new android_socketcan();
        can1.fd = can1.socketcanOpen("can1");
        Log.d("TAG", "onCreate: "+ can1.fd);
        //send
        new Thread() {
            @Override
            public void run() {
                while (true) {
                    try {
                        InputStream inputStream = getResources().openRawResource(R.raw.data);
                        InputStreamReader reader = new InputStreamReader(inputStream);
                        BufferedReader buffer = new BufferedReader(reader);
                        String s;
                        while ((s = buffer.readLine()) != null) {
                            String[] s1 = s.split(" ");
                            String tempS = s1[s1.length - 1];
                            String[] strings = tempS.split("---");
                            strings[0] =
                                    strings[0].trim().substring(8, strings[0].length());
                            strings[1] = strings[1].trim().substring(0, 16);
                            int leng  = can1.socketcanWrite(can1.fd, Long.parseLong(strings[0]), 0, 0, 8, toIntArray(strings[1]));
                            Log.d("TAG", "cansend: "+leng);
                        }
                        sleep(500);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();
    }


    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        return myBinder;
    }

    class MyBinder extends Binder{
        public MyService getService(){
            return MyService.this;
        }
    }

    private int[] toIntArray(String string){
        int[] intArray = new int[8];
        int i = 0;
        while (i < string.length() - 1) {
            int parseInt = Integer.parseInt(string.substring(i, i + 2),16);
            intArray[i / 2] = parseInt;
            i += 2;
        }
        return intArray;
    }
    public static int execRootCmdSilent(String cmd) {
        int result = -1;
        DataOutputStream dos = null;

        try {
            Process p = Runtime.getRuntime().exec("su");
            dos = new DataOutputStream(p.getOutputStream());
            dos.writeBytes(cmd + "\n");
            dos.flush();
            dos.writeBytes("exit\n");
            dos.flush();
            p.waitFor();
            result = p.exitValue();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (dos != null) {
                try {
                    dos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }
}